package com.example.administrator.runbook.fragments.community.fragment;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.runbook.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginDialogFragment extends DialogFragment implements View.OnClickListener {


    public LoginDialogFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        ImageView qq = (ImageView) view.findViewById(R.id.login_qq_fragment);
        ImageView weibo = (ImageView) view.findViewById(R.id.login_weibo_fragment);
        ImageView wechat = (ImageView) view.findViewById(R.id.login_wechat_fragment);
//        qq.setOnTouchListener(this);
//        weibo.setOnTouchListener(this);
//        wechat.setOnTouchListener(this);
        qq.setOnClickListener(this);
        weibo.setOnClickListener(this);
        wechat.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.login_qq_fragment:
                shareQQ();
                break;
            case R.id.login_weibo_fragment:
                shareWeibo();
                break;
            case R.id.login_wechat_fragment:
                shareWeChat();
                break;
        }
    }

//    @Override
//    public boolean onTouch(View v, MotionEvent event) {
//        int action = event.getAction();
//        Animation animation;
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                animation = AnimationUtils.loadAnimation(getContext(), R.anim.image_big_anim);
//                animation.setDuration(1500);
//                v.startAnimation(animation);
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//        }
//        return true;
//    }
public void shareQQ() {
    ShareSDK.initSDK(getContext());
    Platform qq = ShareSDK.getPlatform(QQ.NAME);
    //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
    qq.setPlatformActionListener(new PlatformActionListener() {

        @Override
        public void onError(Platform arg0, int arg1, Throwable arg2) {
            // TODO Auto-generated method stub
            arg2.printStackTrace();
        }

        @Override
        public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
            // TODO Auto-generated method stub
            //输出所有授权信息
            arg0.getDb().exportData();
        }

        @Override
        public void onCancel(Platform arg0, int arg1) {
            // TODO Auto-generated method stub

        }
    });
    //authorize与showUser单独调用一个即可
    qq.authorize();//单独授权,OnComplete返回的hashmap是空的
    qq.showUser(null);//授权并获取用户信息
    //移除授权
    //weibo.removeAccount(true);
}

    public void shareWeibo() {
        ShareSDK.initSDK(getContext());
        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weibo.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                // TODO Auto-generated method stub
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                // TODO Auto-generated method stub
                //输出所有授权信息
                arg0.getDb().exportData();
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        });
        //authorize与showUser单独调用一个即可
        weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    public void shareWeChat() {
        ShareSDK.initSDK(getContext());
        Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weChat.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                // TODO Auto-generated method stub
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                // TODO Auto-generated method stub
                //输出所有授权信息
                arg0.getDb().exportData();
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        });
        //authorize与showUser单独调用一个即可
        weChat.authorize();//单独授权,OnComplete返回的hashmap是空的
        weChat.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }
}
